<template>
  <div class="Search">
    <SearchFilter
      :FilterBrand="FilterBrand"
      :filter="filter"
      @modifyfilter="updatefilter"
    ></SearchFilter>
    <SearchSort :FilterProductlist="FilterProductlist"></SearchSort>
  </div>
</template>

<script>
import SearchFilter from "@/components/SearchFilter.vue";
import SearchSort from "@/components/SearchSort.vue";
import {
  getSearchCategoryFilterList,
  getfilterlist,
  getShoplist,
  getProductByname,
} from "@/apis/getData.js";
export default {
  components: {
    SearchFilter,
    SearchSort,
  },
  data() {
    return {
      tid: -1,
      FilterBrand: [],
      filter: "",
      filterlist: [],
      FilterProductlist: [],
    };
  },
  watch: {
    $route() {
      this.watchrouter();
    },
  },
  mounted() {
      this.watchrouter();
  },
  methods: {
     async watchrouter() {
      let classify = this.$route.query.classify;
      let tid;
      if (classify != null) {
        let upload = new FormData();
        upload.append("typename", classify);
        await getSearchCategoryFilterList(upload).then((res) => {
          if (res.data.data != null) {
            this.FilterBrand = res.data.data.introduce;
            tid = res.data.data.tid;
            if (tid != null) {
              this.tid = tid;
              let Shopdata = new FormData();
              Shopdata.append("tid", tid);
             getShoplist(Shopdata).then((res) => {
                this.FilterProductlist = res.data.data;
              });
             this.updatefilter(this.filterlist);
            }
          } else {
            let gdata = new FormData();
            gdata.append("gname", classify);
            getProductByname(gdata).then((res) => {
              this.FilterProductlist = res.data.data;
              this.tid=-1;
              this.FilterBrand=null;
            });
          }
        });
      }
    },
    updatefilter(data) {
      if(this.tid==-1){
        this.filterlist = data;
        return;
      }
      this.filterlist = data;
      let upload = new FormData();
      upload.append("tid", this.tid);
      upload.append("filterlist", data);
       getfilterlist(upload).then((res) => {
        this.FilterProductlist = res.data.data;
        window.console.log(this.FilterProductlist);
      });
    },
  },
};
</script>

<style scoped>
</style>